﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Quizzer.Helpers;
using Quizzer.Model;

namespace Quizzer.DataAccess
{
    public static class AccountRepository
    {
        readonly static Dictionary<Guid, Account> _accounts;

        static AccountRepository()
        {
            _accounts = LoadAccounts();
        }

        static Dictionary<Guid, Account> LoadAccounts()
        {
            return Account.Items;
        }

        public static Account CreateAccount(string username, string password)
        {
            var query = from acc in _accounts.Values
                        where acc.Username.ToLower() == username.ToLower()
                        select acc;

            if ( query.Count() > 0 )
                return null;
            else
            {
                Account acc = new Account { Username = username, Password = OtherHelpers.GetMD5(password) };
                _accounts.Add(acc.Id, acc);
                return acc;
            }
        }

        public static bool DeleteAccount(Account acc)
        {
            if (_accounts.Values.Contains(acc))
            {
                _accounts.Remove(acc.Id);
                return true;
            }
            
            return false;
        }

        public static ObservableCollection<Account> GetAccounts()
        {
            return new ObservableCollection<Account>(_accounts.Values.ToList());
        }

        //public static Account GetAccount(Guid userId)
        //{
        //    if (_accounts.Keys.Contains(userId))
        //        return _accounts[userId];
        //    else
        //        return null;
        //}
        
        public static bool ContainsUsername(string username)
        {
            var query = from acc in _accounts.Values
                        where acc.Username.ToLower() == username.ToLower()
                        select acc;

            if (query.Count() > 0)
                return true;
            else
                return false;
        }

        public static bool Contains(Account acc)
        {
            if (_accounts.Values.Contains(acc))
                return true;
            else
                return false;
        }

        public static bool CanLogin(string username, string password)
        {
            var query = from acc in _accounts.Values
                        where acc.Username.ToLower() == username.ToLower() && acc.Password == OtherHelpers.GetMD5(password)
                        select acc;

            if (query.Count() > 0)
                return true;
            else
                return false;
        }

        public static Account GetAccount(string username, string password)
        {
            var query = from acc in _accounts.Values
                        where acc.Username.ToLower() == username.ToLower() && acc.Password == OtherHelpers.GetMD5(password)
                        select acc;

            if (query.Count() > 0)
                return query.First();
            else
                return null;
        }

        public static Guid GetID(string username)
        {
            var query = from acc in _accounts.Values
                        where acc.Username.ToLower() == username.ToLower()
                        select acc;

            if (query.Count() > 0)
                return query.First().Id;
            else
                return Guid.Empty;
        }

        //public static bool ChangePassword(Account acc, string oldPassword, string newPassword)
        //{
        //    return false;
        //}
    }
}
